
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
crypto-random-string
Advanced tools
The crypto-random-string package is a utility for generating cryptographically strong random strings of a specified length, using Node.js's crypto module. It can be used for creating unique identifiers, tokens, or any other use cases where random strings are needed.
Generating a random string of a specified length
This feature allows you to generate a random string with a specified length. The length is provided as an option in an object passed to the function.
const cryptoRandomString = require('crypto-random-string');
const randomString = cryptoRandomString({length: 10});
console.log(randomString);
Generating a random string with a specific type
This feature allows you to generate a random string with a specific character set, such as alphanumeric, numeric, or 'url-safe' characters.
const cryptoRandomString = require('crypto-random-string');
const alphanumericString = cryptoRandomString({length: 10, type: 'alphanumeric'});
console.log(alphanumericString);
Generating a random string with custom characters
This feature allows you to generate a random string using a custom set of characters provided by the user.
const cryptoRandomString = require('crypto-random-string');
const customString = cryptoRandomString({length: 10, characters: 'abc123'});
console.log(customString);
The uuid package is used to generate universally unique identifiers (UUIDs). While crypto-random-string generates random strings of a specified length, uuid generates strings in a specific UUID format. UUIDs are often used as database keys, component identifiers, or to ensure uniqueness across distributed systems.
NanoID is a tiny, secure, URL-friendly, unique string ID generator. Similar to crypto-random-string, it can generate random strings of various lengths and character sets. NanoID claims to be a more compact and faster alternative to UUIDs and has a smaller package size compared to crypto-random-string.
Randomatic is a package that generates random strings based on patterns. It allows for more complex patterns and masks than crypto-random-string, such as repeating sequences and placeholders for specific character types (letters, numbers, etc.). This package offers more granular control over the structure of the generated string.
Generate a cryptographically strong random string
Can be useful for creating an identifier, slug, salt, fixture, etc.
$ npm install crypto-random-string
const cryptoRandomString = require('crypto-random-string');
cryptoRandomString(10);
//=> '2cf05d94db'
Returns a hex
string.
Type: number
Length of the returned string.
MIT © Sindre Sorhus
FAQs
Generate a cryptographically strong random string
The npm package crypto-random-string receives a total of 18,718,397 weekly downloads. As such, crypto-random-string popularity was classified as popular.
We found that crypto-random-string demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.